//
//  MyDocument.m
//  LookItUp
//
//  Created by Tim Isted on 23/09/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MyDocument.h"

@implementation MyDocument

- (IBAction)storeValueInDictionary:(id)sender
{
	NSString *keyToStore = [keyToStoreTextField stringValue];
	NSString *valueToStore = [valueToStoreTextField stringValue];
	
	[mainDictionary setValue:valueToStore forKey:keyToStore];
	
	[keyToStoreTextField setStringValue:@""];
	[valueToStoreTextField setStringValue:@""];
}

- (IBAction)retrieveValueFromDictionary:(id)sender
{
	NSString *keyToRetrieve = [keyToRetrieveTextField stringValue];
	
	NSString *retrievedValue = [mainDictionary valueForKey:keyToRetrieve];
	
	if( retrievedValue )
		[retrievedValueLabel setStringValue:retrievedValue];
	else
		[retrievedValueLabel setStringValue:@"Sorry, key not found"];
}

- (id)init
{
    self = [super init];
    if (self) {
       mainDictionary = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)dealloc
{
	[mainDictionary release];
	
	[super dealloc];
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
	 return [mainDictionary writeToURL:absoluteURL atomically:YES];
}

- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
	[mainDictionary release];
	mainDictionary = [[NSMutableDictionary alloc] initWithContentsOfURL:absoluteURL];
	
	return YES;
}

@end
